package com.bdqn.crm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.crm.pojo.SalChance;
import com.bdqn.crm.pojo.SysUser;
import com.bdqn.crm.service.SalChanceService;
import com.bdqn.crm.service.SysUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class SalChanceController {
    @Resource
    private SysUserService sysUserService;

    @Resource
    private SalChanceService salChanceService;

    @RequestMapping(value = "chance/list")
    public String list(Model model,String custName,String chcDesc,Long pageIndex){
        //数据回显
        model.addAttribute("custName",custName);
        model.addAttribute("chcDesc",chcDesc);

        //如果为空，就查询第一页
        if (pageIndex == null){
            pageIndex =1L;
        }
        Page<SalChance> page = new Page<>();
        page.setCurrent(pageIndex);
        IPage<SalChance> salChanceIPage = salChanceService.page(custName,chcDesc,page);
        model.addAttribute("chancePage",salChanceIPage);
        return "chance/list";
    }
    @GetMapping(value = "/chance/add")
    public String add(Model model){
        List<SysUser> list = sysUserService.list();
        model.addAttribute("users",list);
        return "/chance/add";
    }

    @GetMapping(value = "chance/edit")
    public String edit(Model model,Long chcId){
        SalChance salChance = salChanceService.selectId(chcId);   //根据id拿数据
        model.addAttribute("chance",salChance);  //设置到作用域
        //填充下拉框
        List<SysUser> list = sysUserService.list();
        model.addAttribute("users",list);
        return "chance/edit";
    }
    @PostMapping(value = "chance/save")
    public String save(SalChance salChance){
        salChanceService.saveOrUpdate(salChance);
        return "redirect:/chance/list";
    }

    @ResponseBody
    @GetMapping(value = "chance/del/{chcId}")
    public Map del(Long chcId){
        salChanceService.removeById(chcId);
        Map map = new HashMap();
        map.put("delResult","true");
        return map;
    }
}
